package com.tykj.yangningyu.chatdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.im.v2.AVIMClient;
import com.avos.avoscloud.im.v2.AVIMConversation;
import com.avos.avoscloud.im.v2.AVIMException;
import com.avos.avoscloud.im.v2.callback.AVIMClientCallback;
import com.avos.avoscloud.im.v2.callback.AVIMConversationCallback;
import com.avos.avoscloud.im.v2.callback.AVIMConversationCreatedCallback;

import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void createRoom(View view) {
        ChatApp.mClient.open(new AVIMClientCallback(){

            @Override
            public void done(AVIMClient client, AVIMException e){
                if(e==null){
                    //登录成功
                    //创建一个 名为 "HelloKitty PK 加菲猫" 的暂态对话

                    client.createConversation(Collections.<String>emptyList(),"HelloKitty PK 加菲猫",null,true,
                            new AVIMConversationCreatedCallback(){
                                @Override
                                public void done(AVIMConversation conv, AVIMException e){
                                    if (null!=conv){
                                        Intent intent = new Intent(MainActivity.this, ChatActivity.class);
                                        intent.putExtra("conv",conv.getConversationId());
                                        String conversationId = conv.getConversationId();
                                        Log.i("conv", "done: =========="+conversationId);
                                        startActivity(intent);
                                    }else {
                                        Log.i("exception", "done: ======="+e.getMessage());
                                    }
                                }
                            });
                }else {

                }
            }
        });
    }

    public void fetchRoom(View view) {
        AVIMConversation conversation = ChatApp.mClient.getConversation("5870598c128fe1006b274868");
        conversation.join(new AVIMConversationCallback() {
            @Override
            public void done(AVIMException e) {
                Intent intent = new Intent(MainActivity.this, ChatActivity.class);
                intent.putExtra("conv","5870598c128fe1006b274868");
                startActivity(intent);
            }
        });
    }
}
